Page({
    data: {
        userInfo: {},
        phone: '',
        maskedPhone: '',
        userAvatar: 'https://ov-library-1316830564.cos.ap-guangzhou.myqcloud.com/userAvatar/',
    },

    onLoad() {
        const user = wx.getStorageSync('userInfo') || {};
        this.setData({
            userInfo: user,
            phone : this.maskPhone(user.phone)
        });
    },

    onShow(){
        
        this.onLoad()
    },

    onGetPhoneNumber(e) {
        if (e.detail.errMsg === 'getPhoneNumber:ok') {
            wx.login({
                success: loginRes => {
                    wx.request({
                        url: 'http://localhost:8080/api/wx/phone',
                        method: 'POST',
                        data: {
                            code: loginRes.code,
                            encryptedData: e.detail.encryptedData,
                            iv: e.detail.iv
                        },
                        success: res => {
                            if (res.data.code === 200) {
                                const phone = res.data.data;
                                this.setData({
                                    phone,
                                    maskedPhone: this.maskPhone(phone)
                                });
                            } else {
                                wx.showToast({
                                    title: '绑定失败',
                                    icon: 'none'
                                });
                            }
                        }
                    });
                }
            });
        } else {
            wx.showToast({
                title: '用户拒绝授权',
                icon: 'none'
            });
        }
    },

    maskPhone(phone) {
        if (phone) {
             return phone.substring(0, 3) + '****' + phone.substring(7);
        }
       else{
           return 'null';
       }
    },



});